<?php
class Third_Form_Select extends Third_Form_Pattern{

	public function __construct( $name , $options ){
		parent::__construct( $name , $options );
		$this->field = new Zend_Form_Element_Select( $this->name );
		
		if( !empty( $this->options['message'] ) ){
			$this->field->addMultiOption( '', $this->options['message']);
		}
	}

	function setPattern( $pattern ) {
		$pattern = strtolower( $pattern );

		switch( $pattern ){
			case 'enum':
				// $this->options['values'] = array("Sim"=>"S", "Nao"=>"N" );
				$values = Zend_Json::decode( utf8_encode( $this->options['values'] ) );

				$values = Third_Funcs::array_map_tree('utf8_decode', $values );
				
				if( is_array( $values ) )
					$this->field->addMultiOptions( $values );
			break;
			case 'fki':
			case 'fk':
				try{

//					$this->third = Zend_Registry::get('third');

					$qTable	= $this->options['relation'];
					$qKey 	= $this->options['relation_key'];
					$qField = $this->options['relation_field'];

					// ID DA OPERACAO

						$db = Zend_Registry::get('db'); //$db = Zend_Db::factory( );
						
						$select = $db->select()
						    ->from( $qTable,
		                   		 	array(
		                   		 		$qKey,
		                   		 		$qField
		                   		 	)
		                   	)
						    ->order( $qField." ASC " );
						    
						    $stmt = $select->query();
							$result = $stmt->fetchAll();
							if( $result ){
								foreach( $result as $key=>$value){
									$data[ $value[$qKey] ] 	=  $value[ $qField ];
								} 
							}
		
					$this->field->addMultiOptions( $data );
						
				} catch( Exception $e){
					echo $e->getMessage();
				}
			break;
		}
	}
}
?>